/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServerListPanel.java
 *
 * Created on Sep 16, 2011, 6:40:07 PM
 */

package com.agentslab.gameclient;

import aa.core.ActorMessage;
import com.agentslab.constants.APIConstants;
import games.mapacman.client.MaPacman;
import java.util.StringTokenizer;
import javax.swing.JLabel;
import javax.swing.JList;

/**
 *
 * @author mehadihasan
 */
public class ServerListPanel extends javax.swing.JPanel {

    /** Creates new form ServerListPanel */
    public ServerListPanel() {
        initComponents();
    }

    public JLabel getGameNameLabel() {
        return gameNameLabel;
    }

    public JList getServerList() {
        return serverList;
    }

    public void setServerList(JList serverList) {
        this.serverList = serverList;
    }

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        serverList = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        joinButton = new javax.swing.JButton();
        gameNameLabel = new javax.swing.JLabel();
        userNameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();

        jScrollPane1.setViewportView(serverList);

        jLabel1.setText("Following are the available servers for ");

        joinButton.setText("Join");
        joinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                joinButtonActionPerformed(evt);
            }
        });

        gameNameLabel.setText("jLabel2");

        userNameTextField.setText("test2");

        jLabel2.setText("user name");

        jLabel3.setText("password");

        passwordField.setText("test2");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(joinButton))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                                .add(layout.createSequentialGroup()
                                    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 242, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(gameNameLabel))))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(26, 26, 26)
                            .add(jLabel2)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(userNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jLabel3)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(gameNameLabel))
                .add(22, 22, 22)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                .add(19, 19, 19)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(userNameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2)
                    .add(jLabel3)
                    .add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(37, 37, 37)
                .add(joinButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void joinButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_joinButtonActionPerformed
        // TODO add your handling code here:
        StringTokenizer tokenizer = new StringTokenizer((String) this.serverList.getSelectedValue());
        APIConstants.gameName = tokenizer.nextToken();
        APIConstants.serverHost = tokenizer.nextToken();
        Object[] argsreg= new Object[3];
        argsreg[0]= this.userNameTextField.getText();
        argsreg[1] = new String(this.passwordField.getPassword());
        argsreg[2] = ""+System.currentTimeMillis();

        ActorMessage am_reg = new ActorMessage(APIConstants.platform.getActorNameOfPlatform(),
                                               APIConstants.guiActor,
                                               "startClient",
                                               argsreg,
                                               false
                                               );

        APIConstants.platform.sendMessage(am_reg);
    }//GEN-LAST:event_joinButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel gameNameLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton joinButton;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JList serverList;
    private javax.swing.JTextField userNameTextField;
    // End of variables declaration//GEN-END:variables

}
